package com.ym.jiujiangeyou.util;

import java.io.BufferedInputStream;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import com.ym.jiujiangeyou.entity.Parameter;

import android.os.Environment;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 * @Description: Http工具 
 */
public class HttpUtil {

	public HttpUtil() {
	}

	/**
	 * @param url
	 * @return
	 * @retur String
	 * @Description: http get请求
	 */
	public static String httpGet(String url) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		int timeOutConnection = 10000;
		int timeOutSocket = 10000;
		ByteArrayOutputStream outputStream = null;
		InputStream is = null;
		String result = null;
		HttpResponse response = null;
		HttpParams httpParameters = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParameters,
				timeOutConnection);// 设置连接超时
		HttpConnectionParams.setSoTimeout(httpParameters, timeOutSocket);// 设置duqu超时

		try {
			response = client.execute(get);
			outputStream = new ByteArrayOutputStream();
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				is = response.getEntity().getContent();
				byte[] bt = new byte[1024];
				int len = 0;
				while ((len = is.read(bt)) != -1) {
					outputStream.write(bt, 0, len);
				}
				result = outputStream.toString();
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			result = Constants.CONNECTION_TIMEOUT ;
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			result = Constants.CONNECTION_TIMEOUT ;
			e.printStackTrace();
		}
		return result;
	}
	
	/** 
	* @param url
	* @param params
	* @return  
	* @retur  String 
	* @Description: httpPost请求 
	*/
	public static String httpPost(String url, List<Parameter> params) {
		ByteArrayOutputStream outputStream = null;
		InputStream is = null;
		String result = null;
		int timeOutConnection = 10000;
		int timeOutSocket = 10000;
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		HttpParams httpParameters = client.getParams();
		HttpResponse response = null;
		HttpParams parms = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParameters,
				timeOutConnection);// 设置连接超时
		HttpConnectionParams.setSoTimeout(httpParameters, timeOutSocket);// 设置duqu超时
		
			try {
				httpPost.setEntity(new UrlEncodedFormEntity(
						buildNameValuePair(params), HTTP.UTF_8));
				response = client.execute(httpPost);
				outputStream = new ByteArrayOutputStream();
				int statusCode = response.getStatusLine().getStatusCode();
				if (statusCode == HttpStatus.SC_OK) { // status 200
					is = response.getEntity().getContent();
					byte[] bt = new byte[1024];
					int len = 0;
					while ((len = is.read(bt)) != -1) {
						outputStream.write(bt, 0, len);
					}
					result = outputStream.toString();
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				result = Constants.CONNECTION_TIMEOUT ;
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				result = Constants.CONNECTION_TIMEOUT ;
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				result = Constants.CONNECTION_TIMEOUT ;
				e.printStackTrace();
			}
		return result;
	}

	
	/** 
	* @param params
	* @return  
	* @retur  List<BasicNameValuePair> 
	* @Description: 封装参数到 List<BasicNameValuePair> 
	*/
	private static List<BasicNameValuePair> buildNameValuePair(
			List<Parameter> params) {
		List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
		for (Parameter param : params) {
			BasicNameValuePair pair = new BasicNameValuePair(param.getKey(),
					param.getValue());
			result.add(pair);
		}
		return result;
	}
	
	/** 
	* @param urlPath
	* @param fileName
	* @return  
	* @retur  String 
	* @Description: 下载更行程序
	*/
	public static String getInstallPath(String urlPath,String fileName){
		String result = null ;
		byte[] data = null ;
		HttpClient client = new DefaultHttpClient() ;
		HttpGet httpGet = new HttpGet(urlPath) ;
		File file = Environment.getExternalStorageDirectory() ;
		
		FileOutputStream stream = null ;
		HttpResponse response = null ;
		try {
			response = client.execute(httpGet) ;
			data = EntityUtils.toByteArray(response.getEntity()) ;
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
				stream = new FileOutputStream(new File(file, fileName)) ;
				stream.write(data, 0, data.length) ;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = Constants.CONNECTION_TIMEOUT ;
		}finally{
			if(stream != null){
				try {
					stream.close() ;
					result = file+File.separator+fileName ;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return result ;
	}
	
	/** 
	* @param name
	* @param message
	* @retur  String 
	* @Description: 提交反馈信息 
	*/
	public static String SendMessage(String name,String message){
		String result = "";
		Parameter p = new Parameter("name", name);
		Parameter p1 = new Parameter("message", message);
		List<Parameter> param = new ArrayList<Parameter>();
		param.add(p);
		param.add(p1);
		result = httpPost(Constants.SEND_MESSAGE_URL, param) ;
		return result ;
	}

}
